package com.ganbo.pattern.singleton;

/**
 * 双重检查锁模式单利模式
 *
 * @Author: ganbo
 * @Date: 2020/6/8 20:57
 */
public class DoubleCheckSingleton {

    /**
     * 这里用volatile修饰的目的就是禁止指令重排序
     */
    private volatile static DoubleCheckSingleton instance;

    private DoubleCheckSingleton() {
    }

    public static DoubleCheckSingleton getInstance() {
        //检查是否需要阻塞
        if (instance == null) {
            synchronized (DoubleCheckSingleton.class) {
                if (instance == null) {
                    instance = new DoubleCheckSingleton();
                }
            }
        }
        return instance;
    }
}
